package com.game.center;

import com.game.center.entity.Arena;
import com.game.center.respository.ArenaRespository;
import com.game.common.concurrent.GameEventExecutorGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;

import java.util.Optional;

public class AsyncArenaDao extends AbstractAsyncDao {
    private ArenaRespository arenaRespository;


    public AsyncArenaDao(GameEventExecutorGroup executorGroup, ArenaRespository arenaRespository) {
        super(executorGroup);
        this.arenaRespository = arenaRespository;
    }

    public Future<Optional<Arena>> findArena(Long playerId, Promise<Optional<Arena>> promise) {
        this.execute(playerId, promise, () -> {
            Optional<Arena> arena = arenaRespository.findById(String.valueOf(playerId));
            promise.setSuccess(arena);
        });
        return promise;
    }

    public void updateToRedis(long playerId,Arena arena,Promise<Boolean> promise) {
        this.execute(playerId, promise, ()->{
            arenaRespository.saveOrUpdateToRedis(arena, String.valueOf(playerId));
            promise.setSuccess(true);
        });
    }
    public void updateToDB(long playerId,Arena arena,Promise<Boolean> promise) {
        this.execute(playerId,  promise, ()->{
            arenaRespository.saveOrUpdateToDB(arena);
            promise.setSuccess(true);
        });
    }
}
